<?php

namespace App\Store\Controllers\Member;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Member\MemberBalanceChangelog;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Member\Member;

use App\Common\Services\CommonExcelExport;
use App\Store\Models\Member\SiteMemberLv;
use Maatwebsite\Excel\Facades\Excel;
use App\Store\Services\Excel\DownloadTemp;
use Illuminate\Support\Facades\DB;

/**
 * 会员余额信息---预存
 */
class MemberBalanceController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = MemberBalanceChangelog::getList($request);

        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 导出
     */
    public function doexport(Request $request)
    { //isgetall=1;
        $type=[
            '1'=>'余额充值',
            '2'=>'定金充值',
            '3'=>'积分转预存',
            '4'=>'充值赠送',
            '11'=>'购物消费',
            '12'=>'维修开单',
            '13'=>'定金开单消费',
            '99'=>'红冲金额'
        ];
        $list = MemberBalanceChangelog::getList($request)['list'];
        $exceldata = [];
        $row = [
            ['start_store'=>'预存信息列表'],
            [
                'store_name' => '门店名称',
                'real_name' => '会员名称',
                'mobile' => '会员电话',
                // 'lv_name' => '会员等级',
                'type'=>'类型',
                // 'logtitle' => '日志',
                'change_val'=>'金额',
                'create_at'=>'创建时间',
                'create_account_real_name'=>'创建人',
                'remark' => '备注',
                // 'sales_account_name' => '业务员',
                // 'update_account_real_name'=>'更新人',
                // 'update_at'=>'更新时间',
            ]
        ];
        foreach ($list as $val) {
            $exceldata[] = [
                'store_name'=>$val['storeinfo']['store_name'],
                'real_name'=>$val['memberinfo']['real_name']??'',
                'mobile'=>$val['memberinfo']['mobile']??'',
                'type'=>$type[$val['type']],
                // 'logtitle'=>json_decode($val['loginfo'],true)['title'],
                'change_val'=>$val['change_val']/100,
                // 'sales_account_name'=>$val['accountinfo']['realname'],
                'remark'=>$val['remark'],
                'create_account_real_name'=>$val['create_account_real_name'],
                'create_at'=>date('Y-m-d H:i:s',$val['create_at']),
                'update_account_real_name'=>$val['update_account_real_name'],
                'update_at'=>date('Y-m-d H:i:s',$val['update_at'])
            ];
        }

        $filename = '预存信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
